package com.fumin.service.impl;

import com.fumin.mapper.CoinCountMapper;
import com.fumin.mapper.UserMapper;
import com.fumin.pojo.CoinCount;
import com.fumin.service.EhcacheService;
import com.google.code.kaptcha.Producer;

import jnr.ffi.annotations.In;
import net.sf.ehcache.Element;
import org.apache.tools.ant.taskdefs.MacroInstance;

import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Lingo on 2018/2/13.
 */
@Service
public class EhcacheServiceImpl implements EhcacheService {

    @Resource
    EhCacheCacheManager cacheManager;
    @Resource
    UserMapper userMapper;
    @Resource
    CoinCountMapper coinCountMapper;

    @Cacheable(value="fiveMinute", key="#phoneNum")
    public String getIdentifyingCode(String phoneNum,Producer kaptchaProducer) {
        String capText = kaptchaProducer.createText();
        return capText;
    }

    @Override
    public String getIdentifyingCode(String phoneNum) {
        Cache cache = cacheManager.getCache("fiveMinute");
        String value = cache.get(phoneNum,String.class);
        return value;
    }

    @CacheEvict(value="fiveMinute",key="#phoneNum")
    public void removeIdentifyingCode(String phoneNum) {
    }

    @Override
    public boolean checkPhone(String phoneNum) {
        Cache cache = cacheManager.getCache("fiveMinute");
        String value = cache.get(phoneNum,String.class);
        if(value==null){
            return false;
        }else {
            return true;
        }
    }



    @Override
    public Long checkLong() {
        Cache cache = cacheManager.getCache("fiveMinute");
        Long userNum = cache.get("userNum",Long.class);
        if (userNum==null){
            userNum = userMapper.selectNum().get("num");
            cache.put("userNum",userNum);
        }
        return userNum;
    }


    public void updateRankPool() {
        Cache cache = cacheManager.getCache("rankPool");
//        cache.clear();
        List<Map<String,Long>> sqlResults = userMapper.selectRankData();
        for (int i = 0; i <sqlResults.size() ; i++) {
            Map<String,Long> temp= sqlResults.get(i);
            cache.put(temp.get("pk"),i+1);
        }
    }
    @Cacheable(value="rankData", key="#userId")
    public Integer query(Long userId) {
        Cache cache = cacheManager.getCache("rankPool");
        Integer rank = cache.get(userId,Integer.class);
//        if (rank==null){
//            updateRankPool();
//            rank = cache.get(userId,Integer.class);
//        }
        return rank;
    }

    @Override
    public List<Map<String, Object>> selectTop() {
        Cache cache = cacheManager.getCache("fiveMinute");
        List<Map<String, Object>> top10 = cache.get("top10",List.class);
        if (top10==null){
            top10 = userMapper.selectTop();
            cache.put("top10",top10);
        }
        return top10;
    }

    @Override
    public String selectTotal() {
        Cache cache = cacheManager.getCache("fiveMinute");
        String totalCoin = cache.get("totalCoin",String.class);
        if (totalCoin==null){
            Map<String,BigDecimal> total = coinCountMapper.selectTotalCoin();
            totalCoin = total.get("coins").toString();
            cache.put("totalCoin",totalCoin);
        }
        return totalCoin;
    }

    @Override
    public Map<String, Object> selectLast() {
        Cache cache = cacheManager.getCache("fiveMinute");
        Map<String, Object> last = cache.get("last",Map.class);
//        if (last==null){
//            last = userMapper.selectLast();
//            cache.put("last",last);
//        }
        return last;
    }

    @Override
    public  synchronized  void  updateLoginFailedData( String ip) {
        Cache cache = cacheManager.getCache("loginFailed");
        Integer data;
        data = cache.get(ip, Integer.class);
        if (data==null){
            cache.put(ip, 0);
        }else if (data<=9){
            cache.put(ip,data+1);
        }
    }

    @Override
    public synchronized Integer selectFailedData(String ip) {
        Cache cache = cacheManager.getCache("loginFailed");
        Integer data = cache.get(ip, Integer.class);
        return data==null?0:data;
    }
}
